package isssr.bean.boundary;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import org.primefaces.event.RowEditEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import isssr.bean.control.BusinessRoleManagement;
import isssr.bean.entity.Role;

public class BusinessRoleManagementBoundary implements Serializable {

	
	private static final int MAX_LEVEL_NUMBER = 30;
	private String[] levels;
	
	
	private String 	title;
	private String 	description;
	private Integer level;
	private Boolean goalOwnerPermission;
	
	private List<Role> role_list;
	
	
	private LazyDataModel<Role> model;
	
	
	private BusinessRoleManagement ctrl;
	
	
	public void setCtrl(BusinessRoleManagement ctrl) {
		this.ctrl = ctrl;
	}


	public BusinessRoleManagementBoundary() {
		
		
		/*
		this.model = new LazyDataModel<Role>() {
			
			@Override
			public List<Role> load(int first, int pageSize, String sortField, SortOrder sortOrder,
					Map<String, String> filters) {
				
					
				
				
				
				
					
				
				
				
				
				List<Role> retrieved_roles = new ArrayList<Role>();
				
				// valorizzare retrieved_roles in base ai record recuperati dal controllo
					
				
				return retrieved_roles;
			}
		};
		int totalRowCount = 50;
		this.model.setRowCount(totalRowCount);
		
		
		
		
		
		
		
		
		
		
		*/
		System.out.println("entry in business role management boundary");
		this.levels = constructLevelMenu();
		this.role_list = new ArrayList<Role>();
		for(int i=0; i<100; i++)
		{
			Role role = new Role();
			role.setTitle("title-"+i);
			role.setDescription("description-"+i);
			role.setLevel(i);
			role.setGoalOwnerPermission(true);
			this.role_list.add(role);
		}
	}  
	

	private static String[] constructLevelMenu()
	{
		String[] menu = new String[MAX_LEVEL_NUMBER];
		for(int i=0; i<MAX_LEVEL_NUMBER; i++)
			menu[i] = Integer.toString(i+1);
		return menu;
	}
	
	
	public String getTitle() {
		System.out.println("update table");
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}


	public Integer getLevel() {
		return level;
	}


	public void setLevel(Integer level) {
		this.level = level;
	}


	public Boolean getGoalOwnerPermission() {
		return goalOwnerPermission;
	}


	public void setGoalOwnerPermission(Boolean goalOwnerPermission) {
		this.goalOwnerPermission = goalOwnerPermission;
	}

		
	public List<Role> getRole_list() {
		return role_list;
	}


	public void setRole_list(List<Role> role_list) {
		this.role_list = role_list;
	}


	public String[] getLevels() {
		return levels;
	}


	public void setLevels(String[] levels) {
		this.levels = levels;
	}


	public void createRole()
	{
		System.out.println("entry in create role");
		/*
		System.out.println(this.title);
		System.out.println(this.description);
		System.out.println(this.level);
		System.out.println(this.goalOwnerPermission);
		
		
		if(this.ctrl == null)
			System.out.println("ctrl null");
		this.ctrl.insert(this);
		*/
		Role new_role = new Role();
		new_role.setTitle("added-title");
		new_role.setDescription("added-description");
		new_role.setLevel(20);
		new_role.setGoalOwnerPermission(true);
		this.role_list.add(new_role);
		
		System.out.println("list size:\t"+this.role_list.size());
	}
	
	
	public void updateRole(RowEditEvent event) {  
        
		
		
		
		System.out.println("entry in update role");
		
		Role to_update = (Role) event.getObject();
        
		
		if(to_update != null)
    	{
    	
	    	System.out.println(to_update.getTitle());
	    	System.out.println(to_update.getDescription());
	    	System.out.println(to_update.getLevel());
	    	System.out.println(to_update.getGoalOwnerPermission());
    	}
		
		//FacesMessage msg = new FacesMessage("Role Edited", edited_role.getLevel().toString());  
  
        //FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
    
	
    public void deleteRole(Role to_delete) {
    	if(to_delete != null)
    		this.role_list.remove(to_delete);
    	
    	
    	System.out.println("entry in delete role");
    	
    	
    	/*
        FacesMessage msg = new FacesMessage("Car Cancelled", ((Car) event.getObject()).getModel());  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
        */
    }  
	
}
